package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Address;
import com.api.syh56.shengyunhui.entity.Car;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.AddressMapper;
import com.api.syh56.shengyunhui.service.AddressService;
import com.api.syh56.shengyunhui.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

@Slf4j
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper,Address> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;


    @Override
    public void addNewAddress(Address address) {
        log.info("address:{}",address);
        System.out.println("address:"+address);
        //address:Address(id=null, contactName=1, telephone=123456, province=江苏省, city=无锡市, district=锡山区, adCode=320200, address=鼎尚皇冠酒店, uid=null, addressLocation=120.359589,31.590365, isDefault=false, creationTime=null, lastModificationTime=null, isDeleted=null)
//        if (address.getAddress().equals("香港特别行政区") || address.getAddress().equals("澳门特别行政区")) {
//            address.setAddress(address.getCity());
//        }
        //判断是否地址是否为空
        boolean hasEmptyField = CommonUtil.isAnyFieldEmpty(address.getAddress(), address.getDistrict(), address.getCity(),address.getProvince(),
                address.getTelephone(),address.getContactName());
        if(hasEmptyField){
            throw new BaseException("请填写完整信息");
        }

        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        address.setUid(uid);
        //新插入地址如果设为默认
        if(address.getIsDefault()){
            setAlltoDefault(uid);
            saveOrUpdate(address);
        } else {
            saveOrUpdate(address);
        }
    }

    public void removeAddress(Address address) {
        Long id = address.getId();
        Address addressDB = addressMapper.selectById(id);
        //如果删除的car是默认车辆
        if(addressDB.getIsDefault()){
            QueryWrapper<Address> wrapper = new QueryWrapper<>();
            Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
            wrapper.lambda().eq(Address::getUid,uid);
            List<Address> addressList = list(wrapper);
            //如果车辆簿还有其他车辆，将其设置为默认，再删除选中车辆
            if(!addressList.isEmpty()){
                setDefault(addressList.get(0).getId());
            }
        }
        //如果车辆簿没有其他车辆，直接删除
        removeById(id);
    }




    @Override
    public Address selectById(Long id) {
        Address address = addressMapper.selectById(id);
        if (address == null) {
            throw new BaseException("id不存在");
        }
        return address;
    }

    public Page<Address> selectPageByuid(Long uid, Integer pageSize, Integer pageNum) {
        Page<Address> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Address::getUid,uid)
                .orderByDesc(Address::getCreationTime);
        Page<Address> addressPage = addressMapper.selectPage(page, wrapper);
        return addressPage;
    }

    public void setAlltoDefault(Long uid){
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Address::getUid,uid);
        List<Address> lists = list(wrapper);
        lists.forEach(item->{
            item.setIsDefault(false);
        });
        updateBatchById(lists);
    }

    @Override
    public void setDefault(Long id) {
        Address address = addressMapper.selectById(id);
        address.setIsDefault(true);
        updateById(address);
    }

    @Override
    public void updateAddress(Address address) {
        //判断是否地址是否为空
        boolean hasEmptyField = CommonUtil.isAnyFieldEmpty(address.getAddress(), address.getDistrict(), address.getCity(),address.getProvince(),
                address.getTelephone(),address.getContactName());
        if(hasEmptyField){
            throw new BaseException("请填写完整信息");
        }
        if(address.getIsDefault()){
            Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
            setAlltoDefault(uid);
            saveOrUpdate(address);
        } else {
            saveOrUpdate(address);
        }
    }

    @Override
    public Page<Address> selectPageByuidBykeyword(Long uid, Integer pageSize, Integer pageNum, String keyword) {
        Page<Address> page = new Page<>(pageNum,pageSize);
//        QueryWrapper<Address> wrapper = new QueryWrapper<>();
//        wrapper.lambda().eq(Address::getUid,uid);
        Page<Address> addressPage = addressMapper.selectByKeyword(page, uid , keyword);
        return addressPage;
    }

}
